package com.ciwong.epaper.modules.reciteWords.dbreciteword;

import com.ciwong.epaper.common.db.DBCallBack;

import java.util.List;

public abstract class BaseDBWordBookStateManager {
    private String TAG = "BaseDBWordBookStateManager";
    private static BaseDBWordBookStateManager dbManager = null;
    private String NAME = "wordbookstate";

    protected BaseDBWordBookStateManager() {
    }

    public void creatTable() {
        WordBookStateDao wordBookStateDao = new WordBookStateDao();
        wordBookStateDao.creatTable(NAME);
    }

    public void dropTable(String versionId) {
        WordBookStateDao wordBookStateDao = new WordBookStateDao();
        wordBookStateDao.dropTable(NAME + versionId);
    }

    public boolean checkTableIsExist() {
        WordBookStateDao wordBookStateDao = new WordBookStateDao();
        return wordBookStateDao.checkTableIsExist(NAME);
    }

    protected boolean executeAddData(String bookId, int hashCode, DBCallBack dbCallBack) {
        String tableName = NAME;
        WordBookStateDao wordBookStateDao = new WordBookStateDao();
        if (!wordBookStateDao.checkTableIsExist(tableName))
            wordBookStateDao.creatTable(tableName);
        wordBookStateDao.add(tableName, bookId, hashCode);
        return true;
    }

    protected boolean executeAddOrUpdateData(String bookId, int hashCode, DBCallBack dbCallBack) {
        String tableName = NAME;
        WordBookStateDao wordBookStateDao = new WordBookStateDao();
        if (!wordBookStateDao.checkTableIsExist(tableName))
            wordBookStateDao.creatTable(tableName);
        if (wordBookStateDao.queryIsExistBookid(tableName, bookId))
            wordBookStateDao.update(tableName, bookId, hashCode);
        else
            wordBookStateDao.add(tableName, bookId, hashCode);
        return true;
    }

    protected int executeGetByBookId(String bookId) {
        String tableName = NAME;
        WordBookStateDao wordBookStateDao = new WordBookStateDao();
        if (!wordBookStateDao.checkTableIsExist(tableName))
            wordBookStateDao.creatTable(tableName);
        int wordBookUrlHashCode = -1;
        List<Integer> wordBookUrlHashCodeList = wordBookStateDao.queryByBookId(tableName, bookId);
        if (wordBookUrlHashCodeList != null && wordBookUrlHashCodeList.size() > 0)
            wordBookUrlHashCode = wordBookUrlHashCodeList.get(0);
        return wordBookUrlHashCode;
    }

    protected void executeDeleteAll() {
        String tableName = NAME;
        WordBookStateDao wordBookStateDao = new WordBookStateDao();
        if (!wordBookStateDao.checkTableIsExist(tableName))
            wordBookStateDao.creatTable(tableName);
        wordBookStateDao.deleteAll(tableName);
    }

    protected List<Integer> executeQueryAll() {
        String tableName = NAME;
        WordBookStateDao wordBookStateDao = new WordBookStateDao();
        if (!wordBookStateDao.checkTableIsExist(tableName))
            wordBookStateDao.creatTable(tableName);
        List<Integer> wordBookUrlHashCodeList = wordBookStateDao.queryAll(tableName);
        return wordBookUrlHashCodeList;
    }
}
